package com.wayne.gateway.client;

import org.springframework.http.HttpCookie;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;
import org.springframework.web.server.ServerWebExchange;

/**
 * Create date 2020/8/13.
 *
 * @author evan
 */
public class CookieOAuth2Helper {

  public static String BEARER_TYPE = "Bearer";

  public static String OAUTH2_TYPE = "OAuth2";

  public static String SET_COOKIE = "Set-Cookie";

  /** The access token issued by the authorization server. This value is REQUIRED. */
  public static String ACCESS_TOKEN = "access_token";

  /**
   * The type of the token issued as described in <a
   * href="https://tools.ietf.org/html/draft-ietf-oauth-v2-22#section-7.1">Section 7.1</a>. Value is
   * case insensitive. This value is REQUIRED.
   */
  public static String TOKEN_TYPE = "token_type";

  /**
   * The lifetime in seconds of the access token. For example, the value "3600" denotes that the
   * access token will expire in one hour from the time the response was generated. This value is
   * OPTIONAL.
   */
  public static String EXPIRES_IN = "expires_in";

  /**
   * The refresh token which can be used to obtain new access tokens using the same authorization
   * grant as described in <a
   * href="https://tools.ietf.org/html/draft-ietf-oauth-v2-22#section-6">Section 6</a>. This value
   * is OPTIONAL.
   */
  public static String REFRESH_TOKEN = "refresh_token";

  /**
   * The scope of the access token as described by <a
   * href="https://tools.ietf.org/html/draft-ietf-oauth-v2-22#section-3.3">Section 3.3</a>
   */
  public static String SCOPE = "scope";

  public static HttpCookie getAccessTokenCookie(ServerWebExchange exchange) {
    return getCookie(exchange, ACCESS_TOKEN);
  }

  public static HttpCookie getRefreshTokenCookie(ServerWebExchange exchange) {
    return getCookie(exchange, REFRESH_TOKEN);
  }

  private static HttpCookie getCookie(ServerWebExchange exchange, String cookieName) {
    MultiValueMap<String, HttpCookie> cookies = exchange.getRequest().getCookies();
    HttpCookie c = cookies.getFirst(cookieName);
    if (null != c && StringUtils.hasText(c.getValue())) {
      return c;
    }
    return null;
  }
}
